<?php

//后台的文章管理

defined('APP_DIR') or die('404 Not Found');

FLEA::loadClass('Common_Action');

class Controller_AdminArticle extends Common_Action {
	
	private $_modelArticle ;
	
	function __construct($ctlName) {
		parent::__construct ( $ctlName );
		$this->tplDir = TPL_A;//模板位置
		$this->_modelArticle = FLEA::getSingleton('Model_Articles');
	}

 	function index() {
 		$viewData = $this->_modelArticle->getAllArticles ();
		$this->_out('articles/list', $viewData);
	}
	
	function display() {
		
	}
	
	function insert() {
		if ( $this->_isPOST() ) {
			$this->_modelArticle->insertArticle($_POST);
			js_alert ( '添加成功', null, url ( 'adminarticle' ) );
		} else {
			$_modelArticleCatalog = FLEA::getSingleton('Model_ArticleCatalog');
			$formData['catalogs'] = $_modelArticleCatalog->getAllCatalogs();
			$formData['author'] = $_SESSION['RBAC_USERDATA']['USERNAME'];	//当用FLEA_Rbac::getUser()作全局变量时可改掉这个
			//dump(FLEA_Rbac::getUser());
			$viewData = array(
				'form' => $this->_modelArticle->getForm($formData),
				'action' => 'insert'
			);
			//dump($viewData);
			//dump($this->tplDir.'articles/insert.php');
			$this->_out('articles/insert', $viewData);
			//$this->_executeView($this->tplDir.'articles/insert.php', $viewData);
		}
	}
	
	function modify() {
		if ($this->_isPOST ()) {
			$this->_modelArticle->updateArticle($_POST);
			js_alert ( '修改成功', null, url ('adminarticle') );
		} else {
			$articleid = $this->saveGet ( 'article' );
			$article = $this->_modelArticle->getArticleById ( $articleid );
			
			$_modelArticleCatalog = FLEA::getSingleton('Model_ArticleCatalog');
			$article['catalogs'] = $_modelArticleCatalog->getAllCatalogs();
			
			$viewData = array ('form' => $this->_modelArticle->getForm ( $article ), 'action' => 'modify' );
			$this->_out ( 'articles/insert', $viewData );
		}
	}
	
	function delete() {
		//询问是否删除
		$articleid = $this->saveGet ( 'article' );
		$this->_modelArticle->deleteArticleById($articleid);
		js_alert ( '删除成功', null, url ('adminarticle') );
	}
	
	function test() {
		dump($_SESSION);
		//var_dump($_SESSION);
	}
	
}
